package com.shen.accountbook.bean.temp.biz;

import com.shen.accountbook.bean.temp.SecondSortBean;
import com.shen.accountbook.bean.temp.ThirdSortBean;
import com.shen.accountbook.greendao.DaoSession;
import com.shen.accountbook.greendao.SecondSortBeanDao;
import com.shen.accountbook.greendao.ThirdSortBeanDao;
import com.shen.accountbook.greendao.manager.DaoManager;

import java.util.ArrayList;
import java.util.List;

/**
 * author:  shen
 * date:    2018/9/7
 */
public class ThirdSortBeanBiz {

    DaoManager mDaoManager;
    DaoSession mDaoSession;
    ThirdSortBeanDao mDao;
    SecondSortBeanDao mSecondDao;

    public ThirdSortBeanBiz(){
        mDaoManager = DaoManager.getInstance();
        mDaoSession = mDaoManager.getSession();
        mDao = mDaoSession.getThirdSortBeanDao();
        mSecondDao = mDaoSession.getSecondSortBeanDao();
    }

    /**
     * 插入分类
     *
     *
     * @param secondId
     * @param thirdId
     * @param name
     * @param img
     */
    public long insert(long firstId, long secondId, long thirdId, String name, String img){

        long rowId = -1;
        List<ThirdSortBean> list = mDao.queryBuilder().where(
                ThirdSortBeanDao.Properties.Third_sort_id.eq(thirdId)).list();
        if (null == list || list.isEmpty()) {

            ThirdSortBean bean = new ThirdSortBean();
            bean.setId(null);
            bean.setFirst_sort_id(firstId);
            bean.setSecond_sort_id(secondId);
            bean.setThird_sort_id(thirdId);
            bean.setThird_sort_name(name);
            bean.setThird_sort_img(img);

            rowId = mDao.insert(bean);

        } else {
            rowId = list.get(0).getId();
        }

        return rowId;
    }


    /**
     * 1 01 01
     *
     * 插入分类 -- 根据"first_sort_id"加1后 作为first_sort_id插入
     * @param name
     */
    public boolean insertAuto(long firstId, long secondId, String name, String img){

        long rowId;
        List<ThirdSortBean> list = mDao.queryBuilder()
                .orderDesc(ThirdSortBeanDao.Properties.Third_sort_id).list();
        if (null == list || list.isEmpty()) {

            ThirdSortBean bean = new ThirdSortBean();
            bean.setId(null);
            bean.setFirst_sort_id(firstId);
            bean.setSecond_sort_id(secondId);
            bean.setThird_sort_id(firstId * 10000 + secondId * 100 + 10);
            bean.setThird_sort_name(name);
            bean.setThird_sort_img(img);

            rowId = mDao.insert(bean);

        } else {
            ThirdSortBean beanTemp = list.get(0);

            ThirdSortBean bean = new ThirdSortBean();
            bean.setId(null);
            bean.setFirst_sort_id(firstId);
            bean.setSecond_sort_id(secondId);
            bean.setThird_sort_id(beanTemp.getThird_sort_id() + 1);
            bean.setThird_sort_name(name);
            bean.setThird_sort_img(img);

            rowId = mDao.insert(bean);
        }

        if(rowId > 0){
            return true;
        }else {
            return false;
        }
    }


    /**
     * 更新使用频率
     *
     */
    public void updateFrequency(long thirdId){

        List<ThirdSortBean> list = mDao.queryBuilder().where(
                ThirdSortBeanDao.Properties.Third_sort_id.eq(thirdId)).list();
        if (null == list || list.isEmpty()) {

        } else {
            ThirdSortBean bean = list.get(0);
            bean.setFrequency((bean.getFrequency() == null ? 0 : bean.getFrequency()) + 1);
            mDao.update(bean);
        }
    }

    /**
     * 获取全部的
     * @return
     */
    public List<ThirdSortBean> getThirdSortBean(){
        List<ThirdSortBean> beanList = mDao.queryBuilder().list();
        return beanList;
    }

    /**
     * 20个 为一页
     * @param offset
     * @return
     */
    public List<ThirdSortBean> getThirdSortBean(int offset){
        List<ThirdSortBean> beanList = mDao.queryBuilder().offset(offset * 20).limit(20).list();
        return beanList;
    }

    /**
     * 根据第二分类获取第三分类
     * @param secondId
     * @return
     */
    public List<ThirdSortBean> getBeanBySecondId(long secondId){
        List<ThirdSortBean> beanList = mDao.queryBuilder()
                .where(ThirdSortBeanDao.Properties.Second_sort_id.eq(secondId)).list();
        return beanList;
    }



    /**
     * 根据第一分类获取第三分类
     * @param firstId
     * @return
     */
    public List<ThirdSortBean> getBeanByFirstId(long firstId){
        List<ThirdSortBean> beanList = mDao.queryBuilder()
                .where(ThirdSortBeanDao.Properties.First_sort_id.eq(firstId)).list();

        return beanList;
    }

    /**
     * 根据"第一分类获"和"使用频率"取第三分类 -- 使用频率要大于0 -- 拿10个
     * @param firstId
     * @return
     */
    public List<ThirdSortBean> getBeanByFirstIdFrequency(long firstId){
        List<ThirdSortBean> beanList = mDao.queryBuilder()
                .where(ThirdSortBeanDao.Properties.First_sort_id.eq(firstId),
                        ThirdSortBeanDao.Properties.Frequency.gt(0))
                .orderDesc(ThirdSortBeanDao.Properties.Frequency)
                .offset(0).limit(10)
                .list();

        return beanList;
    }

    public List<List<ThirdSortBean>> getBeanByFirstIdSecondId(long firstId) {
        List<SecondSortBean> beanList = mSecondDao.queryBuilder()
                .where(SecondSortBeanDao.Properties.First_sort_id.eq(firstId))
                .list();

        List<List<ThirdSortBean>> listList = new ArrayList<>();
        for(SecondSortBean bean : beanList){
            List<ThirdSortBean> tempList = mDao.queryBuilder()
                    .where(ThirdSortBeanDao.Properties.Second_sort_id.eq(bean.getSecond_sort_id()))
                    .list();

            listList.add(tempList);
        }
        return listList;
    }
}
